package com.wnx.job.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.exception.Asserts;
import com.wnx.job.modules.pms.dto.SendParam;
import com.wnx.job.modules.pms.mapper.PmsSendMapper;
import com.wnx.job.modules.pms.model.PmsSend;
import com.wnx.job.modules.pms.service.PmsSendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Service
public class PmsSendServiceImpl extends ServiceImpl<PmsSendMapper, PmsSend> implements PmsSendService {

    @Resource
    private PmsSendMapper  sendMapper;


    /**
     * 保存投递记录
     * @param send
     */
    @Override
    public void saveSend(PmsSend send) {
        //校验：一个职位只能投递一次，不允许多次投递
        long count = this.finCountByStudentIdAndPostId(send.getStudentId(), send.getPostId());
        if (count > 0) {
            Asserts.fail("该职位，亲您已经投递过了！");
        }

        //校验：一个公司只能投递一个岗位
        long count2 =  this.findCountByStudentIdAndCompanyId(send.getStudentId(),send.getCompanyId());
        if (count2 >= 1){
            Asserts.fail("该公司您已经投递了简历，该公司正在进行人事筛选中，请稍等！");
        }

        //调用持久层，新增简历投递
        sendMapper.insert(send);

    }

    /**
     * 根据学生ID和职位ID查询投递情况
     * @param studentId
     * @param postId
     * @return
     */
    @Override
    public long finCountByStudentIdAndPostId(long studentId, long postId) {
        return sendMapper.selectCount(new QueryWrapper<PmsSend>().eq("student_id",studentId).eq("post_id",postId));
    }

    /**
     *
     * 根据学生ID和企业ID查询投递情况
     * @param studentId
     * @param companyId
     * @return
     */
    @Override
    public long findCountByStudentIdAndCompanyId(long studentId, long companyId) {
            return sendMapper.selectCount(new QueryWrapper<PmsSend>().eq("student_id",studentId).eq("company_id",companyId));
    }
    /**
     * 查询投递简历情况
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<SendParam> findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<SendParam>  sendParams = sendMapper.findByCondition(queryPageBean.getQueryParams());
        return PageResult.restPage(sendParams);
    }

    /**
     * 根据公司ID和学生ID查询简历投递
     * @param companyId
     * @param studentId
     * @return
     */
    @Override
    public PmsSend findSendByCompanyIdAndStudentId(Long companyId, Long studentId) {
        return sendMapper.selectOne(new QueryWrapper<PmsSend>().eq("company_id",companyId).eq("student_id",studentId));
    }
}
